package com.xiaoshuidi.cloud.module.bpm.controller.admin.swo;


import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.bpm.controller.admin.swo.vo.*;
import com.xiaoshuidi.cloud.module.bpm.controller.admin.sworecord.vo.GetSwoTicketRespVO;
import com.xiaoshuidi.cloud.module.bpm.convert.swo.MySwoTicketConvert;
import com.xiaoshuidi.cloud.module.bpm.convert.swo.SwoTicketConvert;
import com.xiaoshuidi.cloud.module.bpm.dal.dataobject.swo.SwoTicketDO;
import com.xiaoshuidi.cloud.module.bpm.service.swo.SwoTicketService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Tag(name = "管理后台 - wo 服务工单申请")
@RestController
@RequestMapping("/bpm/swoTicket")
@Validated
public class SwoTicketController {

    @Resource
    private SwoTicketService swoTicketService;
    @Resource
    private MySwoTicketConvert mySwoTicketConvert;

    @PostMapping("/create")
    @Operation(summary = "创建wo 服务工单申请")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:create')")
    public CommonResult<Long> createSwoTicket(@Valid @RequestBody SwoTicketCreateReqVO createReqVO) {
        Long userId = getLoginUserId();
        LoginUser loginUser = getLoginUser();
        return success(swoTicketService.createSwoTicket(loginUser, createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务工单责任方")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:update')")
    public CommonResult<Boolean> updateSwoTicket(@Valid @RequestBody SwoTicketUpdateReqVO updateReqVO) {
        swoTicketService.updateSwoTicket(getLoginUser(), updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除wo 服务工单申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:delete')")
    public CommonResult<Boolean> deleteSwoTicket(@RequestParam("id") Long id) {
        swoTicketService.deleteSwoTicket(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得wo 服务工单申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:query')")
    public CommonResult<GetSwoTicketRespVO> getSwoTicket(@RequestParam("id") Long id) {
        return success(swoTicketService.getSwoTicket(id));
    }

    @PostMapping("/stop")
    @Operation(summary = "结束wo 服务工单申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:update')")
    public CommonResult<Boolean> stop(@RequestBody @Valid  StopSwoTicketReqVO stopSwoTicketReqVO) {
        Long userId = getLoginUserId();
        return success(swoTicketService.stop(userId, stopSwoTicketReqVO));
    }

    @PostMapping("/shutdown")
    @Operation(summary = "关闭服务工单申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:update')")
    public CommonResult<Boolean> shutdown(@RequestBody @Valid  ShutdownSwoTicketReqVO shutdownSwoTicketReqVO) {
        return success(swoTicketService.shutdown(getLoginUser(), shutdownSwoTicketReqVO));
    }

    @PostMapping("/upload-annex")
    @Operation(summary = "服务工单上传附件")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:update')")
    public CommonResult<Boolean> uploadAnnex(@RequestBody @Valid UploadAnnex uploadAnnex) {
        return success(swoTicketService.uploadAnnex(uploadAnnex));
    }

    @GetMapping("/reminders")
    @Operation(summary = "服务工单-催单")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:update')")
    public CommonResult<Boolean> reminders(@RequestParam("id") Long id) {
        return success(swoTicketService.reminders(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得wo 服务工单申请列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:query')")
    public CommonResult<List<SwoTicketRespVO>> getSwoTicketList(@RequestParam("ids") Collection<Long> ids) {
        List<SwoTicketDO> list = swoTicketService.getSwoTicketList(ids);
        return success(SwoTicketConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得wo 服务工单申请分页")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:query')")
    public CommonResult<PageResult<SwoTicketRespVO>> getSwoTicketPage(SwoTicketPageReqVO pageVO) {
        return success(swoTicketService.getSwoTicketPage(pageVO));
    }

    @GetMapping("/status-count")
    @Operation(summary = "分页状态数量统计")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:query')")
    public CommonResult<SwoTicketStatusCountRespVO> statusCount(Integer serviceType) {
        return success(swoTicketService.statusCount(serviceType));
    }


    @PostMapping("/assignment")
    @Operation(summary = "服务工单 - 指派")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:update')")
    public CommonResult<Long> assignment(@RequestBody @Valid SwoAssignmentReqVO swoAssignmentReqVO){
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return success(swoTicketService.assignment(userId, swoAssignmentReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出wo 服务工单申请 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:export')")
    @OperateLog(type = EXPORT)
    public void exportSwoTicketExcel(@Valid SwoTicketExportReqVO exportReqVO,
                                     HttpServletResponse response) throws IOException {
        List<SwoTicketDO> list = swoTicketService.getSwoTicketList(exportReqVO);
        // 导出 Excel
        List<SwoTicketExcelVO> datas = mySwoTicketConvert.convert(list);
        ExcelUtils.write(response, "wo 服务工单申请.xls", "数据", SwoTicketExcelVO.class, datas);
    }

    @PostMapping("acceptance")
    @Operation(summary = "服务工单受理")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:update')")
    public CommonResult<Boolean> acceptance(@RequestBody @Valid SwoAcceptanceReqVO swoAcceptanceReqVO){
        Boolean acceptance = swoTicketService.acceptance(swoAcceptanceReqVO);
        return CommonResult.success(acceptance);
    }

    @GetMapping("getAcceptanceInfo")
    @Operation(summary = "回显信息")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:query')")
    public CommonResult<SwoAcceptanceReqVO> getAcceptanceInfo(@RequestParam("swoTicketId") Long swoTicketId){
        SwoAcceptanceReqVO swoAcceptanceReqVO = swoTicketService.getAcceptanceInfo(swoTicketId);
        return CommonResult.success(swoAcceptanceReqVO);
    }

    @GetMapping("/pageList")
    @Operation(summary = "获得小程序wo 服务工单申请分页")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:query')")
    public CommonResult<PageResult<SwoTicketRespVO>> getSwoTicketPageList(SwoTicketPageListReqVO pageVO){
        Long loginUserId = getLoginUserId();
        PageResult<SwoTicketRespVO> result =swoTicketService.getSwoTicketPageList(loginUserId, pageVO);
        return success(result);
    }


    @PostMapping("marker")
    @Operation(summary = "小程序wo 标记")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:create')")
    public CommonResult<Boolean> setMarker(@RequestBody @Valid SwoMarkerReqVO swoMarkerReqVO){
        Long userId = getLoginUserId();
        Boolean aBoolean = swoTicketService.setMarker(swoMarkerReqVO, userId);
        return CommonResult.success(aBoolean);
    }

    @PostMapping("bill")
    @Operation(summary = "小程序wo 生成账单")
    @PreAuthorize("@ss.hasPermission('bpm:swoTicket:create')")
    public CommonResult<Boolean> addBill(@RequestBody @Valid SwoBillReqVO swoBillReqVO){
        Boolean aBoolean =swoTicketService.addBill(swoBillReqVO);
        return CommonResult.success(aBoolean);
    }

    @GetMapping("summaryPageList")
    @Operation(summary = "工单数据汇总")
    public CommonResult<PageResult<SwoTicketSummaryCountRespVO>> getSummaryList(@RequestParam(required = false) LocalDate startTime, @RequestParam(required = false) LocalDate endTime, @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        PageResult<SwoTicketSummaryCountRespVO> result = swoTicketService.getSummaryList(startTime,endTime,tenantId,pageSize,pageNum);
        return CommonResult.success(result);
    }
    @PostMapping("/maintenance-rate")
    @Operation(summary = "统计维修完成率")
    public CommonResult<MaintenanceRateRespVO> maintenanceRate(@RequestBody @Valid MaintenanceRateReqVO maintenanceRateReqVO){
        return CommonResult.success(swoTicketService.maintenanceRate(maintenanceRateReqVO));
    }

    @GetMapping("summaryPageListExport")
    @Operation(summary = "工单数据汇总导出")
    public CommonResult<SwoTickerExportResp> summaryListExport(@RequestParam(required = false) LocalDate startTime, @RequestParam(required = false) LocalDate endTime, @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum){
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        SwoTickerExportResp swoTickerExportResp = swoTicketService.getSummaryListExport(startTime,endTime,tenantId,pageSize,pageNum);
        return CommonResult.success(swoTickerExportResp);
    }
    @PostMapping("/is-need-pay-rate")
    @Operation(summary = "统计是否有偿维修")
    public CommonResult<NeedPayRateRespVO> isNeedPayRate(@RequestBody @Valid MaintenanceRateReqVO maintenanceRateReqVO){
        return CommonResult.success(swoTicketService.isNeedPayRate(maintenanceRateReqVO));
    }

    @PostMapping("/evaluate-rate")
    @Operation(summary = "统计评价率")
    public CommonResult<EvaluateRateRespVO> evaluateRate(@RequestBody @Valid MaintenanceRateReqVO maintenanceRateReqVO){
        return CommonResult.success(swoTicketService.evaluateRate(maintenanceRateReqVO));
    }

    @PostMapping("/timely-maintenance-rate")
    @Operation(summary = "统计维修及时率")
    public CommonResult<TimelyMaintenanceRateRespVO> timelyMaintenanceRate(@RequestBody @Valid MaintenanceRateReqVO maintenanceRateReqVO){
        return CommonResult.success(swoTicketService.timelyMaintenanceRate(maintenanceRateReqVO));
    }
}

